package lesson

import "fmt"

func Lesson07() {
	fmt.Println("package lesson file lesson/lesson07.go function Lesson07")

	var slice01 []int
	fmt.Println("[slice01]:", slice01)

	array := [10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
	fmt.Println("[array]:", array)

	slice02 := array[9]
	fmt.Println("[slice02]:", slice02)

	// 赋值的影响
	array[9] = 1
	fmt.Println("array[9] = 1 ")
	fmt.Println("[array]:", array)
	fmt.Println("[slice02]:", slice02)
	array[9] = 0
	slice02 = 1
	fmt.Println("slice02 = 1 ")
	fmt.Println("[array]:", array)
	fmt.Println("[slice02]:", slice02)

	slice03 := array[5:8]
	fmt.Println("[slice03]:", slice03)

	slice04 := array[:5]
	fmt.Println("[slice04]:", slice04)

	slice05 := array[5:]
	fmt.Println("[slice04]:", slice05)

	slice06 := make([]int, 3, 10) //超过长度赋值，新创建长度增加一倍的对象
	fmt.Println("[slice06]:", slice06, " [len(slice06)]:", len(slice06), " [cap(slice06)]:", cap(slice06))
	slice06 = append(slice06, 4, 5, 6, 7, 8, 9, 10, 11)
	fmt.Println("[slice06]:", slice06, " [len(slice06)]:", len(slice06), " [cap(slice06)]:", cap(slice06))

	array1 := []byte{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k'}
	fmt.Println("[array1]:", array1)
	slice07 := array1[2:5]
	fmt.Println("[slice07]:", string(slice07)) // c d e
	slice08 := array1[3:5]
	fmt.Println("[slice08]:", string(slice08)) // d e
	slice09 := slice07[3:5]
	fmt.Println("[slice09]:", string(slice09)) // f g
	slice10 := slice07[1:3]
	fmt.Println("[slice10]:", string(slice10)) // d e
	// slice 是指向连续的内存块
	fmt.Println("[slice07]:", slice07, " [len(slice07)]:", len(slice07), " [cap(slice07)]:", cap(slice07))

	slice11 := make([]int, 3, 6)
	fmt.Printf("[&sclie11]:%p [sclie11]:%v \n", slice11, slice11)
	slice11 = append(slice11, 1, 2, 3)
	fmt.Printf("[&sclie11]:%p [sclie11]:%v \n", slice11, slice11)
	slice11 = append(slice11, 1, 2, 3)
	fmt.Printf("[&sclie11]:%p [sclie11]:%v \n", slice11, slice11)

	array2 := []int{1, 2, 3, 4, 5}
	fmt.Println("[array2]:", array2)
	slice12 := array2[2:5]
	slice13 := array2[1:3]
	fmt.Println("[slice12]:", slice12, " [slice13]:", slice13)
	slice12[0] = 9
	fmt.Println("[slice12]:", slice12, " [slice13]:", slice13)
	fmt.Println("[array2]:", array2)
	slice13 = append(slice13, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
	fmt.Println("[slice12]:", slice12, " [slice13]:", slice13)
	slice12[0] = 10
	fmt.Println("[slice12]:", slice12, " [slice13]:", slice13)
	fmt.Println("[array2]:", array2)

	//copy
	slice14 := []int{1, 2, 3, 4, 5, 6}
	slice15 := []int{7, 8, 9}
	fmt.Println("[slice14]:", slice14, " [slice15]:", slice15)
	copy(slice14, slice15)
	fmt.Println("[slice14]:", slice14, " [slice15]:", slice15)
	slice16 := []int{1, 2, 3, 4, 5, 6}
	slice17 := []int{7, 8, 9}
	fmt.Println("[slice16]:", slice16, " [slice17]:", slice17)
	copy(slice17, slice16)
	fmt.Println("[slice16]:", slice16, " [slice17]:", slice17)

	slice18 := []int{1, 2, 3, 4, 5, 6}
	slice19 := []int{7, 8, 9, 10, 1, 1, 1, 1, 1, 1}
	fmt.Println("[slice18]:", slice18, " [slice19]:", slice19)
	copy(slice19[2:4], slice18[1:3])
	fmt.Println("[slice18]:", slice18, " [slice19]:", slice19)

	// homework
	slice20 := []int{1, 2, 3, 4, 5}
	slice21 := slice20[0:cap(slice20)]
	fmt.Println("[slice20]:", slice20, " [slice21]:", slice21)
}
